<?php
/**
 * 
 */
namespace app\home\controller;

use think\Controller;
use think\Db;

class Base extends Controller
{

    protected $memberInfo;

	public function _initialize(){
        // 系统关闭
        $is_close = model('option')::getOne('home_close');
        if($is_close==1){
            $this->redirect('index/close');
            exit();
        }

    	//登录验证
    	if(is_null(session('member'))){
              $this->redirect('index/logpage');
          }

        define('MID',session('member.mid'));
        define('UID',session('member.id'));
        $this->memberInfo = session('member');

        // 是否是代理中心
        define('IS_AGENT',session('member.is_agent'));

        // 系统前台密钥
        define('HOME_KEY','wxmax@100');
        // 前台手机验证cookie名称
        define('HOME_PHONE_CHECK_COOKIE','JSHOME_PHONE_CHECK_'.md5(HOME_KEY.UID));

        $this->assign('token',self::genToken());
        $this->assign('currentTime',time());
        $this->assign('isAgent',IS_AGENT);
        $this->assign('memberInfo',$this->memberInfo);
	}

    public function updateMemberSession(){
        $member = model('member')->getOneDataById(UID);
        session('member',$member);
    }

	/**
    * 生成token
    */
    public static function genToken() {
        $token_cookie_name = 'JSHOME_TOKENCOOKIE_' . md5(HOME_KEY. session('member.mid'));
        if (isset($_COOKIE[$token_cookie_name])) {
            return $_COOKIE[$token_cookie_name];
        } else {
            $token = md5(getRandStr(16));
            setcookie($token_cookie_name, $token, 0, '/');
            return $token;
        }
    }

    /**
     * 检查token
     */
    public static function checkToken(){
        $token = isset($_REQUEST['token']) ? addslashes($_REQUEST['token']) : '';
        if ($token != self::genToken()) {
            return false;
        }
        return true;
    }

}